
<!DOCTYPE html>
<html lang="">


<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=no">
  <meta name="theme-color" content="#202020">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <script src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" async></script>
  
  
    <meta name="keywords" content>
  

  
    <meta name="description" content="第十七章 JQuery的基础学习">
  
  
  
  <link rel="icon" type="image/x-icon" href="/images/footer-logo.png">
  
  <title>第十七章 JQuery的基础学习 [ 51AIOps 专注于运维自动化  微信： kaipython ]</title>
  
    <!-- stylesheets list from config.yml -->
    
      <link rel="stylesheet" href="//cdn.bootcss.com/pure/1.0.0/pure-min.css">
    
      <link rel="stylesheet" href="/css/xoxo.css">
    
  
</head>


<body>
  <div class="nav-container">
    <nav class="home-menu pure-menu pure-menu-horizontal">
  <a class="pure-menu-heading" href="/">
    
    <span class="title" style="text-transform:none">51AIOps 专注于运维自动化  微信： kaipython</span>
  </a>

  <ul class="pure-menu-list clearfix">
      
          
            
              <li class="pure-menu-item"><a href="/" class="pure-menu-link">首页</a></li>
            
          
      
  </ul>
   
</nav>

  </div>

  <div class="container" id="content-outer">
    <div class="inner" id="content-inner" style='margin-left:-68px!important'>
      <div class="post-container">
  <article class="post" id="post">
    <header class="post-header text-center">
      <h1 class="title">
        第十七章 JQuery的基础学习
      </h1>
      <span>
        
        <time class="time" datetime="2017-05-07T01:55:09.000Z">
        2017-05-07
      </time>
        
      </span>
      <span class="slash">/</span>
      <span class="post-meta">
      <span class="post-tags">
        
      </span>
    </span>
      <span class="slash">/</span>
      <span class="read">
      <span id="busuanzi_value_page_pv"></span> 点击
    </span>
      <span class="slash">/</span>
    </header>

    <div class="post-content">
      <h4 id="JQuery的介绍及其优势"><a href="#JQuery的介绍及其优势" class="headerlink" title="JQuery的介绍及其优势"></a><div id="title">JQuery的介绍及其优势</div></h4><h5 id="1-JQuery的介绍"><a href="#1-JQuery的介绍" class="headerlink" title="1. JQuery的介绍"></a>1. JQuery的介绍</h5><blockquote>
<p>JQuery的宗旨是——WRITE LESS,DO MORE,写更少的代码,做更多的事情。它是轻量级的js库，这是其它的js库所不及的，它兼容CSS3，还兼容各种常用浏览器。</p>
</blockquote>
<blockquote>
<p>JQuery是一个快速的，简洁的JavaScript库，使用户能更方便地处理HTML documents、events、实现动画效果，并且方便地为网站提供AJAX交互。</p>
</blockquote>
<blockquote>
<p>JQuery还有一个比较大的优势是，它的文档说明很全，而且各种应用也说得很详细，同时还有许多成熟的插件可供选择。JQuery能够使用户的html页保持代码和html内容分离，也就是说，不用再在html里面插入一堆js来调用命令了，只需定义id即可</p>
</blockquote>
<h5 id="2-JQuery的优势"><a href="#2-JQuery的优势" class="headerlink" title="2. JQuery的优势"></a>2. JQuery的优势</h5><ul>
<li><p>一款轻量级的js框架。JQuery核心js文件才几十kb，不会影响页面加载速度</p>
</li>
<li><p>丰富的DOM选择器,JQuery的选择器用起来很方便，比如要找到某个dom对象的相邻元素，js可能要写好几行代码，而JQuery一行代码就搞定了，再比如要将一个表格的隔行变色，JQuery也是一行代码搞定</p>
</li>
<li><p>链式表达式。JQuery的链式操作可以把多个操作写在一行代码里，更加简洁</p>
</li>
<li><p>事件、样式、动画支持。JQuery还简化了js操作css的代码，并且代码的可读性也比js要强</p>
</li>
<li><p>Ajax操作支持。JQuery简化了AJAX操作，后台只需返回一个JSON格式的字符串就能完成与前台的通信</p>
</li>
<li><p>跨浏览器兼容。JQuery基本兼容了现在主流的浏览器，不用再为浏览器的兼容问题而伤透脑筋</p>
</li>
<li><p>插件扩展开发。JQuery有着丰富的第三方的插件，例如：树形菜单、日期控件、图片切换插件、弹出窗口等等基本前台页面上的组件都有对应插件，并且用JQuery插件做出来的效果很炫，并且可以根据自己需要去改写和封装插件，简单实用</p>
</li>
</ul>
<h4 id="选择器"><a href="#选择器" class="headerlink" title="选择器"></a><div id="title">选择器</div></h4><h5 id="1-id选择器："><a href="#1-id选择器：" class="headerlink" title="1. id选择器："></a>1. id选择器：</h5><pre><code>$(&quot;#id&quot;)</code></pre><h5 id="2-class选择器："><a href="#2-class选择器：" class="headerlink" title="2. class选择器："></a>2. class选择器：</h5><pre><code>$(&quot;.classname&quot;)</code></pre><h5 id="3-标签选择器"><a href="#3-标签选择器" class="headerlink" title="3. 标签选择器"></a>3. 标签选择器</h5><pre><code>$(&quot;tagname&quot;)</code></pre><h5 id="4-组合选择器"><a href="#4-组合选择器" class="headerlink" title="4. 组合选择器"></a>4. 组合选择器</h5><pre><code>$(&quot;#id, .classname, tagname&quot;)</code></pre><h5 id="5-层级选择器"><a href="#5-层级选择器" class="headerlink" title="5. 层级选择器"></a>5. 层级选择器</h5><pre><code>$(&quot;#id a&quot;) ----&gt; 所有的后代a元素
$(&quot;#id &gt; a&quot;)---&gt; 第一个儿子元素</code></pre><h5 id="6-基本选择器"><a href="#6-基本选择器" class="headerlink" title="6. 基本选择器"></a>6. 基本选择器</h5><pre><code>:first :eq() :last</code></pre><h5 id="7-属性选择器"><a href="#7-属性选择器" class="headerlink" title="7. 属性选择器"></a>7. 属性选择器</h5><pre><code>&lt;input type=&apos;text&apos;&gt;
&lt;input type=&apos;checkbox&apos;&gt;
&lt;input type=&apos;password&apos;&gt;

$(&quot;input[type=&apos;checkbox&apos;]&quot;)</code></pre><p><strong>实例：表格的全选和反选以及取消</strong>    </p>
<div style="display:none;">
#### <div id="title">筛选器</div>

<h5 id="1-下一个元素"><a href="#1-下一个元素" class="headerlink" title="1. 下一个元素"></a>1. 下一个元素</h5><pre><code>$(&quot;#one&quot;).next()
$(&quot;#one&quot;).nextAll()
$(&quot;#one&quot;).nextUntil(&apos;#i2&apos;)</code></pre><h5 id="2-上一个元素"><a href="#2-上一个元素" class="headerlink" title="2. 上一个元素"></a>2. 上一个元素</h5><pre><code>$(&apos;#one&apos;).prev()
$(&apos;#one&apos;).prevAll()
$(&apos;#one&apos;).prevUntil(&apos;#ii1&apos;)</code></pre><h5 id="3-父亲元素"><a href="#3-父亲元素" class="headerlink" title="3. 父亲元素"></a>3. 父亲元素</h5><pre><code>$(&apos;#one&apos;).parent()
$(&apos;#one&apos;).parents()
$(&apos;#one&apos;).parentsUntil()</code></pre><h5 id="4-儿子和兄弟元素"><a href="#4-儿子和兄弟元素" class="headerlink" title="4. 儿子和兄弟元素"></a>4. 儿子和兄弟元素</h5><pre><code>$(&apos;#one&apos;).children()
$(&apos;#one&apos;).siblings()</code></pre><h5 id="5-查找元素"><a href="#5-查找元素" class="headerlink" title="5. 查找元素"></a>5. 查找元素</h5><pre><code>$(&apos;#one&apos;).find() </code></pre><p><strong>案例：左侧菜单</strong></p>
</div>

<hr>
<h4 id="样式操作"><a href="#样式操作" class="headerlink" title="样式操作"></a><div id="title">样式操作</div></h4><h5 id="1-添加整块样式"><a href="#1-添加整块样式" class="headerlink" title="1. 添加整块样式"></a>1. 添加整块样式</h5><pre><code>addClass()</code></pre><h5 id="2-删除整块样式"><a href="#2-删除整块样式" class="headerlink" title="2. 删除整块样式"></a>2. 删除整块样式</h5><pre><code>removeClass()</code></pre><h5 id="3-判断样式存不存在"><a href="#3-判断样式存不存在" class="headerlink" title="3. 判断样式存不存在"></a>3. 判断样式存不存在</h5><pre><code>hasClass()</code></pre><h5 id="4-相当于开关，有这个样式则去掉，没有则添加"><a href="#4-相当于开关，有这个样式则去掉，没有则添加" class="headerlink" title="4. 相当于开关，有这个样式则去掉，没有则添加"></a>4. 相当于开关，有这个样式则去掉，没有则添加</h5><pre><code>toggleClass()</code></pre><hr>
<h4 id="文本操作"><a href="#文本操作" class="headerlink" title="文本操作"></a><div id="title">文本操作</div></h4><h5 id="1-对纯文本操作"><a href="#1-对纯文本操作" class="headerlink" title="1. 对纯文本操作"></a>1. 对纯文本操作</h5><pre><code>$(&quot;#one&quot;).text(); --- 获取值
$(&quot;#one&quot;).text(&apos;fuck&apos;); --- 设置值</code></pre><h5 id="2-对HTML操作"><a href="#2-对HTML操作" class="headerlink" title="2. 对HTML操作"></a>2. 对HTML操作</h5><pre><code>$(&quot;xxx&quot;).html()
$(&quot;xxx&quot;).html(&apos;&lt;a href=&quot;&quot;&gt;&lt;/a&gt;&apos;)    </code></pre><h5 id="3-对val操作（针对input系列）"><a href="#3-对val操作（针对input系列）" class="headerlink" title="3. 对val操作（针对input系列）"></a>3. 对val操作（针对input系列）</h5><pre><code>$(&quot;xxx&quot;).val()
$(&quot;xxx&quot;).val(&apos;123&apos;)</code></pre><hr>
<h4 id="属性操作"><a href="#属性操作" class="headerlink" title="属性操作"></a><div id="title">属性操作</div></h4><h5 id="1-用于自定义属性"><a href="#1-用于自定义属性" class="headerlink" title="1. 用于自定义属性"></a>1. 用于自定义属性</h5><pre><code>$(&apos;xxx&apos;).attr(&apos;n&apos;)
$(&apos;xxx&apos;).attr(&apos;n&apos;,&apos;v&apos;)
$(&apos;xxx&apos;).removeAttr(&apos;n&apos;)</code></pre><h5 id="2-用于checkbox，radio"><a href="#2-用于checkbox，radio" class="headerlink" title="2. 用于checkbox，radio"></a>2. 用于checkbox，radio</h5><pre><code>$(&apos;xxx&apos;).prop(&apos;checked&apos;)
$(&apos;xxx&apos;).prop(&apos;checked&apos;, true)</code></pre><p><strong>注意：</strong></p>
<blockquote>
<p><code>&lt;input type=&#39;checkbox&#39; id=&#39;i1&#39; checked=checked /&gt;</code><br>            此处选中复选框的时候，我们可以直接使用attr来进行赋值操作，但是在1以及2版本的jquery中会出错，在3版本的jquery中没有任何问题</p>
</blockquote>
<blockquote>
<p>因此，我们在处理checkbox，radio等标签的时候，我们尽量使用特定的prop，不要使用attr(“checked”,”checked”)</p>
</blockquote>
<hr>
<div style="display:none">
#### <div id="title">文档处理</div>

<h5 id="1-插入到指定元素”里面”的后面"><a href="#1-插入到指定元素”里面”的后面" class="headerlink" title="1. 插入到指定元素”里面”的后面"></a>1. 插入到指定元素”里面”的后面</h5><pre><code>$(&apos;xxx&apos;).append()
$(&apos;xxx&apos;).appendTo()</code></pre><h5 id="2-插入到指定元素”里面”的前面"><a href="#2-插入到指定元素”里面”的前面" class="headerlink" title="2. 插入到指定元素”里面”的前面"></a>2. 插入到指定元素”里面”的前面</h5><pre><code>$(&apos;xxx&apos;).prepend()
$(&apos;xxx&apos;).prependTo()</code></pre><h5 id="3-插入到指定元素”外面”的后面"><a href="#3-插入到指定元素”外面”的后面" class="headerlink" title="3. 插入到指定元素”外面”的后面"></a>3. 插入到指定元素”外面”的后面</h5><pre><code>$(&apos;xxx&apos;).after()</code></pre><h5 id="4-插入到指定元素”外面”的前面"><a href="#4-插入到指定元素”外面”的前面" class="headerlink" title="4. 插入到指定元素”外面”的前面"></a>4. 插入到指定元素”外面”的前面</h5><pre><code>$(&apos;xxx&apos;).before()</code></pre><h5 id="5-移除和清空元素"><a href="#5-移除和清空元素" class="headerlink" title="5. 移除和清空元素"></a>5. 移除和清空元素</h5><pre><code>$(&apos;xxx&apos;).remove()
$(&apos;xxx&apos;).empty()</code></pre><h5 id="6-替换元素"><a href="#6-替换元素" class="headerlink" title="6. 替换元素"></a>6. 替换元素</h5><pre><code>$(&apos;xxx&apos;).replaceWith()
$(&apos;xxx&apos;).replaceAll()</code></pre><p><strong>案例：两个框之间互相选择</strong></p>
<h4 id="动画效果"><a href="#动画效果" class="headerlink" title="动画效果"></a><div id="title">动画效果</div></h4><pre><code>show()
hide()
toggle()

fadeIn()
fadeOut()
fadeToggle()

slideIn()
slideOut()
slideToggle()</code></pre><p><strong>案例：图片的淡入和淡出</strong></p>
</div>

<h4 id="事件"><a href="#事件" class="headerlink" title="事件"></a><div id="title">事件</div></h4><h5 id="1-常见的事件"><a href="#1-常见的事件" class="headerlink" title="1. 常见的事件"></a>1. 常见的事件</h5><pre><code>$(&apos;xxx&apos;).click(function(){})
$(&apos;xxx&apos;).mouseout(function(){})
$(&apos;xxx&apos;).mouseover(function(){})
$(&apos;xxx&apos;).hover(function(){})
$(&apos;xxx&apos;).blur(function(){})          
$(&apos;xxx&apos;).focus(function(){})</code></pre><h5 id="2-页面加载完成之后，执行函数体中的内容"><a href="#2-页面加载完成之后，执行函数体中的内容" class="headerlink" title="2. 页面加载完成之后，执行函数体中的内容"></a>2. 页面加载完成之后，执行函数体中的内容</h5><pre><code>$(function(){})
$(document).ready(function(){})</code></pre><h4 id="案例"><a href="#案例" class="headerlink" title="案例"></a>案例</h4><figure class="highlight htmlbars"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line"><span class="xml"><span class="meta">&lt;!DOCTYPE html&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">"en"</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">"UTF-8"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Title<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span>   <span class="attr">onclick</span>=<span class="string">"selectAll();"</span> <span class="attr">value</span>=<span class="string">"全选"</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span>  <span class="attr">onclick</span>=<span class="string">"cancelAll();"</span> <span class="attr">value</span>=<span class="string">"取消"</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"button"</span>  <span class="attr">onclick</span>=<span class="string">"reverseAll();"</span> <span class="attr">value</span>=<span class="string">"反选"</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;<span class="name">table</span> <span class="attr">border</span>=<span class="string">"1px"</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>192.168.1.1<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>80<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>111111<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">class</span>=<span class="string">"check"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>192.168.1.2<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>81<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>222222<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">class</span>=<span class="string">"check"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>192.168.1.3<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>82<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span>333333<span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">            <span class="tag">&lt;<span class="name">td</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"checkbox"</span> <span class="attr">class</span>=<span class="string">"check"</span>&gt;</span><span class="tag">&lt;/<span class="name">td</span>&gt;</span></span></span><br><span class="line"><span class="xml">        <span class="tag">&lt;/<span class="name">tr</span>&gt;</span></span></span><br><span class="line"><span class="xml">    <span class="tag">&lt;/<span class="name">table</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span> <span class="attr">src</span>=<span class="string">"https://code.jquery.com/jquery-3.5.1.js"</span>&gt;</span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml">    function selectAll()&#123;</span></span><br><span class="line"><span class="xml">        // 1. 找到所有的checkbox</span></span><br><span class="line"><span class="xml">        var inpts = $('.check');</span></span><br><span class="line"><span class="xml">       // console.log(inpts);</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        // 2. 对找到的checkbox进行打钩</span></span><br><span class="line"><span class="xml">        for (var i = 0; i <span class="tag">&lt; <span class="attr">inpts.length</span>; <span class="attr">i</span>++)&#123;</span></span></span><br><span class="line"><span class="xml">//            console.log(inpts[i].checked);</span></span><br><span class="line"><span class="xml">            inpts[i].checked = true;</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml">    &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    function cancelAll()&#123;</span></span><br><span class="line"><span class="xml">        // 1. 找到所有的checkbox</span></span><br><span class="line"><span class="xml">        var inpts = $('.check');</span></span><br><span class="line"><span class="xml">//        console.log(inpts);</span></span><br><span class="line"><span class="xml">        // 2. 对找到的checkbox进行打钩</span></span><br><span class="line"><span class="xml">        for (var i = 0; i <span class="tag">&lt; <span class="attr">inpts.length</span>; <span class="attr">i</span>++)&#123;</span></span></span><br><span class="line"><span class="xml">//            console.log(inpts[i].checked);</span></span><br><span class="line"><span class="xml">            inpts[i].checked = false;</span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml">    &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">    function reverseAll()&#123;</span></span><br><span class="line"><span class="xml">        var inpts = $('.check');</span></span><br><span class="line"><span class="xml">//        console.log(inpts);</span></span><br><span class="line"><span class="xml">        // 2. 对找到的checkbox进行打钩</span></span><br><span class="line"><span class="xml">        for (var i = 0; i <span class="tag">&lt; <span class="attr">inpts.length</span>; <span class="attr">i</span>++)&#123;</span></span></span><br><span class="line"><span class="xml">           if (inpts[i].checked)&#123;</span></span><br><span class="line"><span class="xml">               inpts[i].checked = false;</span></span><br><span class="line"><span class="xml">           &#125;else&#123;</span></span><br><span class="line"><span class="xml">               inpts[i].checked = true;</span></span><br><span class="line"><span class="xml">           &#125;</span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml">        &#125;</span></span><br><span class="line"><span class="xml">    &#125;</span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span></span><br><span class="line"><span class="xml"></span></span><br><span class="line"><span class="xml"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span></span><br></pre></td></tr></table></figure>


    </div>

  </article>
  <div class="toc-container">
    
  <div id="toc" class="toc-article">
    <strong class="toc-title">目录</strong>
    <ol class="toc"><li class="toc-item toc-level-4"><a class="toc-link" href="#JQuery的介绍及其优势"><span class="toc-text">JQuery的介绍及其优势</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-JQuery的介绍"><span class="toc-text">1. JQuery的介绍</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-JQuery的优势"><span class="toc-text">2. JQuery的优势</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#选择器"><span class="toc-text">选择器</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-id选择器："><span class="toc-text">1. id选择器：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-class选择器："><span class="toc-text">2. class选择器：</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-标签选择器"><span class="toc-text">3. 标签选择器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-组合选择器"><span class="toc-text">4. 组合选择器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-层级选择器"><span class="toc-text">5. 层级选择器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-基本选择器"><span class="toc-text">6. 基本选择器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#7-属性选择器"><span class="toc-text">7. 属性选择器</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-下一个元素"><span class="toc-text">1. 下一个元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-上一个元素"><span class="toc-text">2. 上一个元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-父亲元素"><span class="toc-text">3. 父亲元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-儿子和兄弟元素"><span class="toc-text">4. 儿子和兄弟元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-查找元素"><span class="toc-text">5. 查找元素</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#样式操作"><span class="toc-text">样式操作</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-添加整块样式"><span class="toc-text">1. 添加整块样式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-删除整块样式"><span class="toc-text">2. 删除整块样式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-判断样式存不存在"><span class="toc-text">3. 判断样式存不存在</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-相当于开关，有这个样式则去掉，没有则添加"><span class="toc-text">4. 相当于开关，有这个样式则去掉，没有则添加</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#文本操作"><span class="toc-text">文本操作</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-对纯文本操作"><span class="toc-text">1. 对纯文本操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-对HTML操作"><span class="toc-text">2. 对HTML操作</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-对val操作（针对input系列）"><span class="toc-text">3. 对val操作（针对input系列）</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#属性操作"><span class="toc-text">属性操作</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-用于自定义属性"><span class="toc-text">1. 用于自定义属性</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-用于checkbox，radio"><span class="toc-text">2. 用于checkbox，radio</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#1-插入到指定元素”里面”的后面"><span class="toc-text">1. 插入到指定元素”里面”的后面</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-插入到指定元素”里面”的前面"><span class="toc-text">2. 插入到指定元素”里面”的前面</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#3-插入到指定元素”外面”的后面"><span class="toc-text">3. 插入到指定元素”外面”的后面</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#4-插入到指定元素”外面”的前面"><span class="toc-text">4. 插入到指定元素”外面”的前面</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#5-移除和清空元素"><span class="toc-text">5. 移除和清空元素</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#6-替换元素"><span class="toc-text">6. 替换元素</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#动画效果"><span class="toc-text">动画效果</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#事件"><span class="toc-text">事件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#1-常见的事件"><span class="toc-text">1. 常见的事件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#2-页面加载完成之后，执行函数体中的内容"><span class="toc-text">2. 页面加载完成之后，执行函数体中的内容</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#案例"><span class="toc-text">案例</span></a></li></ol>
  </div>


  </div>
</div>
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=5sr5du46f27&amp;m=0&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33" async="async"></script>
<div class="copyright">
    <span>本作品采用</span>
    <a href="https://creativecommons.org/licenses/by/4.0/">知识共享署名 4.0 国际许可协议</a>
    <span>进行许可。 转载时请注明原文链接。</span>
</div>


  
    <div class="post-nav" style="margin-left:-168px;">
      <div class="post-nav-item post-nav-next">
        
          <span>〈 </span>
          <a href="/2017/04/07/爬取链家二手房信息/" rel="next" title="爬链家二手房数据">
          爬链家二手房数据
          </a>
        
      </div>
  
      <div class="post-nav-item post-nav-prev">
          
          <a href="/2017/05/07/html相关概念/" rel="prev" title="第十四章 HTML基础学习目录">
            第十四章 HTML基础学习目录
          </a>
          <span>〉</span>
        
      </div>
    </div>
  


	
	<div style="width:109%; margin-left:-144px" id="lv-container" data-id="city" data-uid="MTAyMC80OTg5NS8yNjM4Ng==">
	<script type="text/javascript">
   	   (function(d, s) {
       		var j, e = d.getElementsByTagName(s)[0];

       		if (typeof LivereTower === 'function') { return; }

       		j = d.createElement(s);
       		j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
       		j.async = true;

       		e.parentNode.insertBefore(j, e);
   	   })(document, 'script');
	</script>
	<noscript> 为正常使用来必力评论功能请激活JavaScript</noscript>
        </div>
	



    </div>

    

  </div>
  <footer class="footer text-center">
    <div id="bottom-inner">
        <a class="bottom-item" href target="_blank">GitHub</a> |
        <a class="bottom-item" href>友情链接</a> |
        <a class="bottom-item" href="https://hexo.io" target="_blank">Powered by hexo</a> |
        <a class="bottom-item" href="https://github.com/fooying/hexo-theme-xoxo-plus" target="_blank">Theme xoxo-plus</a> |
        <a class="bottom-item" href="/atom.xml">订阅</a>
    </div>
</footer>

  

<script>
  (function(window, document, undefined) {

    var timer = null;

    function returnTop() {
      cancelAnimationFrame(timer);
      timer = requestAnimationFrame(function fn() {
        var oTop = document.body.scrollTop || document.documentElement.scrollTop;
        if (oTop > 0) {
          document.body.scrollTop = document.documentElement.scrollTop = oTop - 50;
          timer = requestAnimationFrame(fn);
        } else {
          cancelAnimationFrame(timer);
        }
      });
    }

    var hearts = [];
    window.requestAnimationFrame = (function() {
      return window.requestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
        window.mozRequestAnimationFrame ||
        window.oRequestAnimationFrame ||
        window.msRequestAnimationFrame ||
        function(callback) {
          setTimeout(callback, 1000 / 60);
        }
    })();
    init();

    function init() {
      css(".heart{z-index:9999;width: 10px;height: 10px;position: fixed;background: #f00;transform: rotate(45deg);-webkit-transform: rotate(45deg);-moz-transform: rotate(45deg);}.heart:after,.heart:before{content: '';width: inherit;height: inherit;background: inherit;border-radius: 50%;-webkit-border-radius: 50%;-moz-border-radius: 50%;position: absolute;}.heart:after{top: -5px;}.heart:before{left: -5px;}");
      attachEvent();
      gameloop();
      addMenuEvent();
    }

    function gameloop() {
      for (var i = 0; i < hearts.length; i++) {
        if (hearts[i].alpha <= 0) {
          document.body.removeChild(hearts[i].el);
          hearts.splice(i, 1);
          continue;
        }
        hearts[i].y--;
        hearts[i].scale += 0.004;
        hearts[i].alpha -= 0.013;
        hearts[i].el.style.cssText = "left:" + hearts[i].x + "px;top:" + hearts[i].y + "px;opacity:" + hearts[i].alpha + ";transform:scale(" + hearts[i].scale + "," + hearts[i].scale + ") rotate(45deg);background:" + hearts[i].color;
      }
      requestAnimationFrame(gameloop);
    }

    /**
     * 给logo设置点击事件
     * 
     * - 回到顶部
     * - 出现爱心
     */
    function attachEvent() {
      var old = typeof window.onclick === "function" && window.onclick;
      var logo = document.getElementById("logo");
      if (logo) {
        logo.onclick = function(event) {
          returnTop();
          old && old();
          createHeart(event);
        }
      }
      
    }

    function createHeart(event) {
      var d = document.createElement("div");
      d.className = "heart";
      hearts.push({
        el: d,
        x: event.clientX - 5,
        y: event.clientY - 5,
        scale: 1,
        alpha: 1,
        color: randomColor()
      });
      document.body.appendChild(d);
    }

    function css(css) {
      var style = document.createElement("style");
      style.type = "text/css";
      try {
        style.appendChild(document.createTextNode(css));
      } catch (ex) {
        style.styleSheet.cssText = css;
      }
      document.getElementsByTagName('head')[0].appendChild(style);
    }

    function randomColor() {
      // return "rgb(" + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + "," + (~~(Math.random() * 255)) + ")";
      return "#F44336";
    }

    function addMenuEvent() {
      var menu = document.getElementById('menu-main-post');
      if (menu) {
        var toc = document.getElementById('toc');
        if (toc) {
          menu.onclick = function() {
            if (toc) {
              if (toc.style.display == 'block') {
                toc.style.display = 'none';
              } else {
                toc.style.display = 'block';
              }
            }
          };
        } else {
          menu.style.display = 'none';
        }
      }
    }

  })(window, document);
</script>

  



  

</body>
</html>
